<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class M_backend_user extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->library('pb4php/Pb4php');
        $this->load->library('proto/Proto');
        $this->load->library('zmq/ZmqMDClient', array());

        $mdClient = $this->config->item('mdClient');
        $verbose = $mdClient['verbose'];
        $brokerArray = $mdClient['brokerList'];
        $currentBroker = 0;
        $this->mdpClient = new ZmqMDClient($brokerArray[$currentBroker], $verbose);
        $this->mdpClient->connect_to_broker();
    }

    function backend_login($type) {
        $params = array();
        $this->load->library('zmq/ZmqMDClient', $params);
        $mdClient = $this->config->item('mdClient');
        $verbose = $mdClient['verbose'];
        $brokerArray = $mdClient['brokerList'];
        $currentBroker = 0;
	$email = '';
	$password = '';
	// acx backend login account list
        $acxLogin = $this->config->item('acxLogin');

	if(isset($acxLogin[$type]) && isset($acxLogin[$type]['email']) && isset($acxLogin[$type]['password'])){
		$email = $acxLogin[$type]['email'];
		$password = $acxLogin[$type]['password'];
	}else{
		echo "You do not have permission to take this action";
		exit();
	}
        /**
         * Message Login
         */
        $loginRequest = New LoginRequest();
        $loginRequest->set_email($email);
        $loginRequest->set_password($password);
        $loginRequest->set_login_method(LoginMethod::EMAIL);
        $messageLoginRequest = $loginRequest->SerializeToString();

        /**
         * Message user
         */
        $uuid = uniqid();
        $userLogin = New User();
        $userLogin->set_uuid($uuid);
        $userLogin->set_action(User_ActionType::LOGIN);
        $userLogin->set_message($messageLoginRequest);
        $userLogin->set_undefined_action_type(false);
        $messageUserLogin = $userLogin->SerializeToString();

        $messageRequest = new zmsg();
        $messageRequest->body_set($messageUserLogin);
        $this->mdpClient = new ZmqMDClient($brokerArray[$currentBroker], $verbose);
        $this->mdpClient->connect_to_broker();
        $userLoginReply = $this->mdpClient->send("accounts", $messageRequest);

        if (!$userLoginReply) {
            echo "Failed to connect to the account service.";
            exit;
        } else {
            $userReply = New User();
            $userReply->ParseFromString($userLoginReply->last());
            $loginReply = New LoginReply();
            $loginReply->ParseFromString($userReply->message());
            if ($loginReply->error_no() == 0) {
                if($type == ''){
                    $type = 'acx';
                }

                $uid = $this->session->userdata('backendUid');
                $uid[$type] = $loginReply->profile()->uid();

                $session = $this->session->userdata('backendSessionKey');
                $session[$type] = $loginReply->session_key();

                $this->session->set_userdata('backendSessionKey', $session);
                $this->session->set_userdata('backendUid', $uid);
            } else{
                echo $loginReply->error_msg();
            	exit;
            }
        }
    }

}
